//
//  ATRAZMapDataLoader.m
//  ABCTime
//
//  Created by lianglibao on 2018/6/23.
//  Copyright © 2018年 100TAL. All rights reserved.
//

#import "ATRAZMapDataLoader.h"
#import "ATRAZMapCardModel.h"
#import "ATUtil.h"

@implementation ATRAZMapDataLoader

+ (instancetype)shareInstance {
    static dispatch_once_t onceToken;
    static ATRAZMapDataLoader *shareInstance;
    dispatch_once(&onceToken, ^{
        shareInstance = [[ATRAZMapDataLoader alloc] init];
    });
    return shareInstance;
}

- (instancetype)init {
    if (self = [super init]) {

    }
    return self;
}

- (void)clearCache {
    self.dataSource = nil;
    
}

- (NSArray *)loadRazMapData {
//    [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass = 117;
//    [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_passing = 32;
    NSString *deviceString;
    if ([ATUtil isiPhoneXR]) {
        deviceString = @"iPhoneXR";
    } else if ([ATUtil isiPhoneX]) {
        deviceString = @"iPhoneX";
    } else if ([ATUtil isiPhoneSize_5_5]) {
        deviceString = @"iPhone5_5";
    } else if ([ATUtil isiPhoneSize_4_7]) {
        deviceString = @"iPhone4_7";
    } else if ([ATUtil isiPhoneSize_4_0]) {
        deviceString = @"iPhone4_0";
    } else if ([ATUtil isiPhoneSize_3_5]) {
        deviceString = @"iPhone3_5";
    } else {
        deviceString = @"iPad";
    }
    
    NSString *basePath = $(@"%@/RAZ_Maps/%@/", [ATUtil getCachePath], deviceString);
    
    UIImage *cardDontImage;
    UIImage *cardDoneImage;
    UIImage *guidePostImage;
    NSMutableArray *tempModels = [NSMutableArray array];
    UIImage   *bgCoverImage;
    UIImage *cardSuperDontImage;
    UIImage *cardSuperDoneImage;
    UIImage *stationImage;
    UIImage *waterDoneImage;
    UIImage *waterDontImage;
    __block NSInteger cardStarIndex = 0;
    NSString *cardPointpath;
    NSData *cardPointData;
    NSArray *rectArray;
    NSMutableArray *bgImages = [NSMutableArray array];
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat subjectWidth = 0;
    int count = 7;
    NSString *sourcePath;
    UIImage *image;
    if (!self.dataSource.count) {
        
        for (int i =0; i < count; i++) {
            sourcePath = $(@"%@%@%@_%d.png", basePath, deviceString, @"_subject_one_bg", i);
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
            subjectWidth += image.size.width;
            if (image) {
                [bgImages addObject:image];
            }
        }
        
        // 加载card Dont image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_normal_card_dont.png");
        cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card Done image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_normal_card_done.png");
        cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDoneImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_super_card_done.png");
        cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDontImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_super_card_dont.png");
        cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载站台 stationImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_whirligig_station.png");
        stationImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载水中关卡图片(可点击)
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_water_card_done.png");
        waterDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载水中关卡图片(不可可点击)
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_water_card_dont.png");
        waterDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card point
        cardPointpath = $(@"%@%@%@", basePath, deviceString, @"_subject_one_card_point.json");
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        NSError *error;
        rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData options:0 error:&error];
        NSLog(@"%@", error);
        
        // 设置cardStarIndex
        cardStarIndex = 0;
        
        // 生成model
        ATRAZMapCardModel *modelOne = [ATRAZMapCardModel new];
        modelOne.subjectWidth    = subjectWidth;
        modelOne.cardDontImage   = cardDontImage;
        modelOne.cardDoneImage   = cardDoneImage;
        modelOne.cardCenterFrame = rectArray;
        modelOne.cardStartIndex  = cardStarIndex;
        // modelOne.curPassNum      = [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass;
        modelOne.guidePostImage  = guidePostImage;
        modelOne.cardSuperDontImage = cardSuperDontImage;
        modelOne.cardSuperDoneImage = cardSuperDoneImage;
        modelOne.cardStationImage   = stationImage;
        modelOne.cardWaterDoneImage = waterDoneImage;
        modelOne.cardWaterDontImage = waterDontImage;
        modelOne.bgImages           = bgImages.copy;
        
        [tempModels addObject:modelOne];
        self.dataSource = tempModels.copy;
    }
    
// 第2个地图数据====================================================
    if ((self.dataSource.count == 1)) {
        // 更新所有subject Model 数组
        if (!tempModels.count) {
            [tempModels addObjectsFromArray:self.dataSource];
        }
    
        // 更新第二个subject Model 的开始起点值
        [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            cardStarIndex += obj.cardCenterFrame.count;
        }];
    
        
        [bgImages removeAllObjects];
        subjectWidth = 0.0;
        for (int i =0; i < count; i++) {
            sourcePath = $(@"%@%@%@_%d.png", basePath, deviceString, @"_subject_two_bg", i);
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
            subjectWidth += image.size.width;
            if (image) {
                [bgImages addObject:image];
            }
        }
        
        // 加载cell连接处cover bg
        bgCoverImage = [UIImage imageNamed:@"subject_two_cover_bg"];

        // 加载card Dont image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_two_dont.png");
        cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card Done image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_two_done.png");
        cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDontImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_two_super_dont.png");
        cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDoneImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_two_super_done.png");
        cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card point
        cardPointpath = $(@"%@%@%@", basePath, deviceString, @"_subject_two_card_point.json");
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData options:0 error:nil];
        
        // 生成model
        ATRAZMapCardModel *modelTwo = [ATRAZMapCardModel new];
        modelTwo.subjectWidth    = subjectWidth;
        modelTwo.cardDontImage   = cardDontImage;
        modelTwo.cardDoneImage   = cardDoneImage;
        modelTwo.cardCenterFrame = rectArray;
        modelTwo.cardStartIndex  = cardStarIndex;
        // modelTwo.curPassNum      = [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass;
        modelTwo.guidePostImage  = guidePostImage;
        modelTwo.bgLayerImage    = bgCoverImage;
        modelTwo.cardSuperDontImage = cardSuperDontImage;
        modelTwo.cardSuperDoneImage = cardSuperDoneImage;
        modelTwo.bgImages           = bgImages.copy;
    
        [tempModels addObject:modelTwo];
        self.dataSource = tempModels.copy;
    }

// 第3个地图数据====================================================
    if ((self.dataSource.count == 2)) {
        if (!tempModels.count) {
            [tempModels addObjectsFromArray:self.dataSource];
        }
        // 更新第二个subject Model 的开始起点值
        cardStarIndex = 0;
        [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            cardStarIndex += obj.cardCenterFrame.count;
        }];
    
        [bgImages removeAllObjects];
        subjectWidth = 0.0;
        for (int i =0; i < count; i++) {
            sourcePath = $(@"%@%@%@_%d.png", basePath, deviceString, @"_subject_three_bg", i);
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
            subjectWidth += image.size.width;
            if (image) {
                [bgImages addObject:image];
            }
        }

        // 加载cell连接处cover bg
        bgCoverImage = [UIImage imageNamed:@"subject_three_cover_bg"];

        // 加载card Dont image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_three_dont.png");
        cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card Done image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_three_done.png");
        cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDontImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_three_super_dont.png");
        cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDoneImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_three_super_done.png");
        cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card point
        cardPointpath = $(@"%@%@%@", basePath, deviceString, @"_subject_three_card_point.json");
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData options:0 error:nil];
    
        // 生成model
        ATRAZMapCardModel *modelThree = [ATRAZMapCardModel new];
        modelThree.subjectWidth    = subjectWidth;
        modelThree.cardDontImage   = cardDontImage;
        modelThree.cardDoneImage   = cardDoneImage;
        modelThree.cardCenterFrame = rectArray;
        modelThree.cardStartIndex  = cardStarIndex;
        // modelThree.curPassNum      = [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass;
        modelThree.guidePostImage  = guidePostImage;
        modelThree.cardSuperDontImage = cardSuperDontImage;
        modelThree.cardSuperDoneImage = cardSuperDoneImage;
        modelThree.bgLayerImage       = bgCoverImage;
        modelThree.bgImages           = bgImages.copy;
        
        [tempModels addObject:modelThree];
        self.dataSource = tempModels.copy;
    }

// 第4个地图数据====================================================
    // 加载第4个地图数据
    if ((self.dataSource.count == 3)) {
        if (!tempModels.count) {
            [tempModels addObjectsFromArray:self.dataSource];
        }
        // 更新第二个subject Model 的开始起点值
        cardStarIndex = 0;
        [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            cardStarIndex += obj.cardCenterFrame.count;
        }];
        
        [bgImages removeAllObjects];
        subjectWidth = 0.0;
        for (int i =0; i < count; i++) {
            sourcePath = $(@"%@%@%@_%d.png", basePath, deviceString, @"_subject_four_bg", i);
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
            subjectWidth += image.size.width;
            if (image) {
                [bgImages addObject:image];
            }
        }
        
        // 加载cell连接处cover bg
        bgCoverImage = [UIImage imageNamed:@"subject_four_cover_bg"];
        
        // 加载card Dont image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_four_dont.png");
        cardDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card Done image
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_four_done.png");
        cardDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDontImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_four_super_dont.png");
        cardSuperDontImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card cardSuperDoneImage
        sourcePath = $(@"%@%@%@", basePath, deviceString, @"_subject_four_super_done.png");
        cardSuperDoneImage = [UIImage imageWithContentsOfFile:sourcePath];
        
        // 加载card point
        cardPointpath = $(@"%@%@%@", basePath, deviceString, @"_subject_four_card_point.json");
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData options:0 error:nil];
        
        // 生成model
        ATRAZMapCardModel *modelFour = [ATRAZMapCardModel new];
        modelFour.subjectWidth    = subjectWidth;
        modelFour.cardDontImage   = cardDontImage;
        modelFour.cardDoneImage   = cardDoneImage;
        modelFour.cardCenterFrame = rectArray;
        modelFour.cardStartIndex  = cardStarIndex;
        // modelFour.curPassNum      = [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass;
        modelFour.guidePostImage  = guidePostImage;
        modelFour.cardSuperDontImage = cardSuperDontImage;
        modelFour.cardSuperDoneImage = cardSuperDoneImage;
        modelFour.bgLayerImage       = bgCoverImage;
        modelFour.bgImages           = bgImages.copy;
    
        [tempModels addObject:modelFour];
        self.dataSource = tempModels.copy;
    }

// 第5个地图数据====================================================
    if ((self.dataSource.count == 4)) {
        if (!tempModels.count) {
            [tempModels addObjectsFromArray:self.dataSource];
        }
        // 更新第二个subject Model 的开始起点值
        cardStarIndex = 0;
        [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            cardStarIndex += obj.cardCenterFrame.count;
        }];

        [bgImages removeAllObjects];
        subjectWidth = 0.0;
        for (int i =0; i < count; i++) {
            sourcePath = $(@"%@%@%@_%d.png", basePath, deviceString, @"_subject_five_bg", i);
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
                subjectWidth += image.size.width;
            if (image) {
                [bgImages addObject:image];
            }
        }

        // 加载card point
        cardPointpath = $(@"%@%@%@", basePath, deviceString, @"_subject_five_card_point.json");
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData options:0 error:nil];

    
        // 生成model 与 第2个model相同
        ATRAZMapCardModel *model2 = self.dataSource[1];
        ATRAZMapCardModel *modelFive = [ATRAZMapCardModel new];
        modelFive.cardStartIndex  = cardStarIndex;
        modelFive.subjectWidth    = subjectWidth;
        modelFive.cardDontImage   = model2.cardDontImage;
        modelFive.cardDoneImage   = model2.cardDoneImage;
        modelFive.cardCenterFrame = rectArray;
        modelFive.cardStartIndex  = cardStarIndex;
        // modelFive.curPassNum      = [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass;
        modelFive.cardSuperDontImage = model2.cardSuperDontImage;
        modelFive.cardSuperDoneImage = model2.cardSuperDoneImage;
        modelFive.bgLayerImage       = model2.bgLayerImage;
        modelFive.bgImages           = bgImages.copy;
    
        [tempModels addObject:modelFive];
        self.dataSource = tempModels.copy;
    }

// 第6个地图数据====================================================
    if ((self.dataSource.count == 5)) {
        if (!tempModels.count) {
            [tempModels addObjectsFromArray:self.dataSource];
        }
        // 更新第二个subject Model 的开始起点值
        cardStarIndex = 0;
        [tempModels enumerateObjectsUsingBlock:^(ATRAZMapCardModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            cardStarIndex += obj.cardCenterFrame.count;
        }];
        
        [bgImages removeAllObjects];
        subjectWidth = 0.0;
        for (int i =0; i < count; i++) {
            sourcePath = $(@"%@%@%@_%d.png", basePath, deviceString, @"_subject_six_bg", i);
            image = [UIImage imageWithContentsOfFile:sourcePath];
            image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:UIImageOrientationUp];
            subjectWidth += image.size.width;
            if (image) {
                [bgImages addObject:image];
            }
        }
        
        // 加载card point
        cardPointpath = $(@"%@%@%@", basePath, deviceString, @"_subject_six_card_point.json");
        cardPointData = [NSData dataWithContentsOfFile:cardPointpath];
        rectArray = [NSJSONSerialization JSONObjectWithData:cardPointData options:0 error:nil];
    
    
        // 生成model 与 第2个model相同
        ATRAZMapCardModel *model3 = self.dataSource[2];
        ATRAZMapCardModel *modelSix = [ATRAZMapCardModel new];
        modelSix.cardStartIndex  = cardStarIndex;
        modelSix.subjectWidth    = subjectWidth;
        modelSix.cardDontImage   = model3.cardDontImage;
        modelSix.cardDoneImage   = model3.cardDoneImage;
        modelSix.cardCenterFrame = rectArray;
        modelSix.cardStartIndex  = cardStarIndex;
        // modelSix.curPassNum      = [ATRAZMapModelManager sharedInstance].localRazMapModel.cur_pass;
        modelSix.cardSuperDontImage = model3.cardSuperDontImage;
        modelSix.cardSuperDoneImage = model3.cardSuperDoneImage;
        modelSix.bgLayerImage       = model3.bgLayerImage;
        modelSix.bgImages           = bgImages.copy;
    
        [tempModels addObject:modelSix];
        self.dataSource = tempModels.copy;

    }

    return self.dataSource;
}

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
}



- (void)updateDataSource {

}

@end
